#include <stdio.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
// #include "Resource.h"
#include "topyunplus/Config.h"

SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *surface = NULL;
SDL_Texture *texture = NULL;
Mix_Music *music = NULL;
TTF_Font *font = NULL;

#define PNG_FILE_NAME  "assets/image/bg.png"
#define music_FILE_NAME "assets/music/06_Battle_in_Space_Intro.ogg"
#define font_FILE_NAME "assets/font/VonwaonBitmap-16px.ttf"


void drawIMG()
{
    surface = IMG_Load(PNG_FILE_NAME);
    if (surface == nullptr) {
        SDL_Log("IMG_Load Error: %s", SDL_GetError());
        // break;
    }
    texture = SDL_CreateTextureFromSurface(renderer, surface);

    SDL_RenderCopy(renderer, texture, nullptr, nullptr);  
    IMG_Quit();
}

void playSound()
{
    // SDL_Mixer Initialize
    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) 
    {
        SDL_Log("Mix_OpenAudio Error: %s", SDL_GetError());
        // break;
    }
    // read music
    music = Mix_LoadMUS(music_FILE_NAME);
    
    Mix_PlayMusic(music, -1);

    Mix_FreeMusic(music);
    
    Mix_CloseAudio();
    Mix_Quit();
}

void drawText()
{
    // load font
    SDL_Color color = {0, 255, 0, 255};
    font = TTF_OpenFont(font_FILE_NAME, 16);
    
    surface = TTF_RenderUTF8_Solid(font, "Hello, SDL! 中文也可以！ ", color);
    texture = SDL_CreateTextureFromSurface(renderer, surface);

    SDL_Rect textRect = {100, 100, surface->w, surface->h};
    SDL_RenderCopy(renderer, texture, NULL, &textRect);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    TTF_CloseFont(font);
    TTF_Quit();
}

int main(int args, char *argv[])
{
    // SDL 初始化
    if (SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Can not init video, %s", SDL_GetError);
        return 1;
    }

    // 创建窗口
    window = SDL_CreateWindow("SDL2 Learn", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == NULL)  {
        SDL_Log("Can not create Window, %s", SDL_GetError());
        return 1;
    }

    // 创建渲染器
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == NULL) {
        SDL_Log("Can not Create Renderer, %s", SDL_GetError());
        return 1;
    }

    // SDL_image 初始化
    if (IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) != (IMG_INIT_PNG | IMG_INIT_JPG)) {
        SDL_Log("Can not Init image, %s", SDL_GetError());
        return 1;
    }

    // SDL_TTF Initialize
    if (TTF_Init() != 0) {
        SDL_Log("TTF_Init Error: %s", SDL_GetError());
    }

    // SDL_Mixer Initialize
    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) 
    {
        SDL_Log("Mix_OpenAudio Error: %s", SDL_GetError());
        return 1;
    }

    SDL_RenderClear(renderer);
    drawIMG();
    // load font
    SDL_Color color = {0, 255, 0, 255};
    font = TTF_OpenFont(font_FILE_NAME, 16);
    
    surface = TTF_RenderUTF8_Solid(font, "Play Sound！ ", color);
    texture = SDL_CreateTextureFromSurface(renderer, surface);

    SDL_Rect textRect = {100, 100, surface->w, surface->h};
    SDL_RenderCopy(renderer, texture, NULL, &textRect);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    TTF_CloseFont(font);
    TTF_Quit();

    SDL_RenderPresent(renderer);
    
    // read music
    music = Mix_LoadMUS(music_FILE_NAME);
    Mix_PlayMusic(music, -1);

    while (true) {
        SDL_Event event;
        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                break;
            }
        }
    }
    Mix_FreeMusic(music);
    
    Mix_CloseAudio();
    Mix_Quit();
    
    SDL_FreeSurface(surface);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
}
